package ztc.servlet;

import ztc.common.StuBean;
import ztc.service.QRStatus;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.ConcurrentHashMap;


/**
 * Created by ztc on 16-4-6.下午7:10
 */
public class QRAjax extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决乱码
        response.setCharacterEncoding("utf-8");
        ServletContext application=this.getServletContext();
        HttpSession session=request.getSession(false);
        PrintWriter out=response.getWriter();
        ConcurrentHashMap<String,ConcurrentHashMap<String,StuBean>> aMap=
                (ConcurrentHashMap<String, ConcurrentHashMap<String, StuBean>>) application.getAttribute("sessionID");
        ConcurrentHashMap<String,StuBean> sMap=
                aMap.get(session.getId());
        int time=0;
        if(session!=null) {
            while (true) {
                time++;
                if (QRStatus.isReflash(application, session)) {
                    StuBean stu=sMap.get(session.getAttribute("uuid"));
                    //System.out.println(sMap.size()+";"+stu.getStuID()+";"+stu.getName()+";"+stu.getSex());
                    out.println(sMap.size()+";"+stu.getStuID()+";"+stu.getName()+";"+stu.getMac());
                    break;
                }else{
                    if(time>=10){
                        out.print("failed");
                        break;
                    }else {
                        try {
                            //保持住
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
